import platform = require('../../../../platform/common/baseCommon');
import data = require('../../../../platform/server/data');
import kfjsonrpc = require('../../../../platform/server/jsonRpc/JsonRpc');
import dbconnector = require('../../../../platform/server/dataService');
import monment = require('moment');
import appServer = require('../../../../platform/server/appServer');


let thirdService = new dbconnector.dataTableService();
//服务地址
//thirdService.serviceUrl = 'http://192.168.5.23:8080/ATS5S/HS/JsonRpc/SQLForNodeService';
//thirdService.serviceUrl = 'http://192.168.0.186:8080/ATS55_PT/HS/JsonRpc/SQLForNodeService';
thirdService.serviceUrl = 'http://192.168.5.39:18080/ATSDT_SRV/HS/JsonRpc/SQLForNodeService';


//thirdService.dbconnect = new dbconnector.dbInfoAndConnands();
//数据库连接信息
// let connectInfo = new dbconnector.connectionInfo('ATS55', 'jdbc:oracle:thin:@192.168.5.30:1521/ats60', 'PAO',
//     '3L2o1ngae3591', 'oracle.jdbc.driver.OracleDriver');


let connectInfo = new dbconnector.connectionInfo('ATS55', 'jdbc:oracle:thin:@192.168.5.36:1521/ats55', 'system',
    'system', 'oracle.jdbc.driver.OracleDriver');

// let connectInfo = new dbconnector.connectionInfo('ATS55', 'jdbc:oracle:thin:@192.168.5.30:1521/ats60', 'SYSTEM',
//     'hXmNDg1tSam', 'oracle.jdbc.driver.OracleDriver');
//mysql
// let connectInfo = new dbconnector.connectionInfo('world', 'jdbc:mysql://localhost:3306/world', 'root',
//     'root', 'com.mysql.jdbc.Driver');
thirdService.dbConnectInfo = connectInfo;
thirdService.sqlGenerator = new dbconnector.baseDmlStatementGenerator();
//thirdService.sqlGenerator = new dbconnector.mysqlGenerator();
//测试查询语句
var queryTest = new data.dataCommandInfo();
queryTest.id = 'queryTest';
//queryTest.sql = 'SELECT * FROM TNMS.T_USER T';
//queryTest.sql = 'SELECT * FROM TNMS.T_POSITION T';
//queryTest.sql = `SELECT  * FROM TNMS.T_PERMISSION T  where 1=1`;
//queryTest.sql = 'SELECT * FROM TNMS.T_EVENT T';
queryTest.sql = 'SELECT * FROM TNMS.T_BIG_DATA';
queryTest.tableName = 'TNMS.T_PERMISSION';
queryTest.isMultitableQuery = false;

thirdService.commandList = [];
thirdService.commandList.push(queryTest);

/**
 * 测试数据更新
 */
export function testUpdateTable(): Promise<any> {
    return thirdService.query('queryTest', null, 0, 10).then((dataTable: platform.dataTable) => {
        //dataTable.name = 'TNMS.T_USER ';
        // dataTable.primaryKey = [];
        // dataTable.primaryKey.push('ID');
        // dataTable.rows[0].rowValues['TEL'] = '163';

        // dataTable.primaryKey = [];
        // dataTable.primaryKey.push('USER_ID');
        //dataTable.primaryKey.push('ROLE_ID');

        //var d = new Date(dataTable.rows[0].rowValues['PERMISSION_DATE']);


        //测试时间的修改
        //初始化时间
        // var time = new monment(dataTable.rows[0].rowValues['PERMISSION_DATE']);
        // var testTime = time.format('YYYY-MM-DD HH:mm:ss');
        // console.log(testTime);
        // dataTable.rows[0].rowValues['DISABLE_TAG'] = '1';
        // dataTable.rows[0].rowValues['PERMISSION_TARGET'] = '887';
        // dataTable.rows[0].rowValues['PERMISSION_DATE'] = testTime;


        var testObject = JSON.stringify(dataTable.rows[0].rowValues);
        dataTable.rows[0].dataRowStatus = 'Modified';


        thirdService.updateTable(dataTable).then(data => {
            console.log(data);
            return data;
        });

    }).then(data => {
        return null;
    });

};


/**
 * 测试插入数据
 */
export function testInsertTable(): Promise<any> {
    return thirdService.query('queryTest', null, 0, 500).then((dataTable: platform.dataTable) => {
        dataTable.name = 'TNMS.T_USER ';
        dataTable.primaryKey = [];
        dataTable.primaryKey.push('ID');
        dataTable.addRow(1);
        let rowlength = dataTable.rows.length;
        (dataTable.rows[rowlength - 1].rowValues)['ID'] = 'Node.jsTest';
        dataTable.rows[rowlength - 1].setdataRowStatus(platform.dataRowState.added);
        thirdService.updateTable(dataTable).then(data => {
            console.log(data);
            return data;
        });
    }).then(data => {
        return null;
    });
};


/**
 * 测试sql 语句查询并发
 */
export function testQuery() {

    //let sqls = ['SELECT * FROM TNMS.T_EVENT T', 'SELECT * FROM TNMS.T_POSITION T', 'SELECT * FROM TNMS.T_TERMINAL'];
    //let sqls = ['select * from city t', 'select * from country t'];
    let sqls = ['select * from tester.T_USER T where 1=1'];
    for (var i = 0; i < 1; i++) {
        for (let key of sqls) {
            thirdService.commandList[0].sql = key;
            thirdService.commandList[0].tableName = 'tester.T_USER';
            thirdService.commandList[0].isMultitableQuery = false;
            thirdService.query('queryTest', null, 0, 1).then((dataTable: platform.dataTable) => {
                if (dataTable) {
                    for (var j = 0; j < dataTable.primaryKey.length; j++) {
                        console.log(dataTable.primaryKey[j]);
                    }
                    console.log('查询完毕');
                } else {
                    console.log('查询失败');
                }
            }).catch(Error => {
                console.log(Error);
            });
        }

    }

}

/**
 * 测试 sql 语句删除
 */
export function testDelete() {
    //先查询表回来
    return thirdService.query('queryTest', null, 0, 500).then((dataTable: platform.dataTable) => {
        dataTable.name = 'T_USER ';
        dataTable.primaryKey = [];
        dataTable.primaryKey.push('ID');
        //获取某一行数据,并把行状态改变
        for (let key of dataTable.rows) {
            if (key.rowValues['ID'] == 'Node.jsTest') {
                key.dataRowStatus = platform.dataRowState.deleted;
            }
        }
        thirdService.updateTable(dataTable).then(data => {
            console.log(data);
            return data;
        });
    }).then(data => {
        return null;
    });
}


/**
 * 测试查询表格信息
 */
export function testQueryInfo() {
    thirdService.queryTableInfo('T_USER').then((data: platform.DataTableInfo) => {
        for (let key of data.keyField)
            console.log(key);

    })
}


/**
 * 测试多条数据进行编辑
 */
export function testMultipleData() {

    //先查询数据
    thirdService.query('queryTest', null, 0, 500).then((dataTable: platform.dataTable) => {

        //修改数据
        dataTable.name = 'T_USER ';
        dataTable.primaryKey = [];
        dataTable.primaryKey.push('ID');
        //获取某一行数据,并把行状态改变(修改数据)
        dataTable.name = 'TNMS.T_USER ';
        dataTable.primaryKey = [];
        dataTable.primaryKey.push('ID');
        dataTable.rows[0].rowValues['TEL'] = '@163';
        dataTable.rows[0].dataRowStatus = 'Modified';

        //增加数据 两条
        dataTable.addRow(1);
        let rowlength = dataTable.rows.length;
        (dataTable.rows[rowlength - 1].rowValues)['ID'] = 'Node.jsTest';
        dataTable.rows[rowlength - 1].setdataRowStatus(platform.dataRowState.added);


        dataTable.addRow(1);
        rowlength = dataTable.rows.length;
        (dataTable.rows[rowlength - 1].rowValues)['ID'] = 'Node.jsTest2';
        dataTable.rows[rowlength - 1].setdataRowStatus(platform.dataRowState.added);

        //删除数据 一条
        //获取某一行数据,并把行状态改变
        for (let key of dataTable.rows) {
            if (key.rowValues['ID'] == 'Node.jsTest') {
                key.setdataRowStatus(platform.dataRowState.deleted);
            }
        }

        //执行更新
        thirdService.updateTable(dataTable).then(data => {
            console.log(data);
            return data;
        });
    }).then(data => {
        return null;
    });
}

/**
 * 测试加密类
 */
export function testencryptionUtility() {

    let messgae: string = 'hellow world';

    console.log(appServer.encrypt.calMd5(messgae));

    console.log(appServer.encrypt.calSha1(messgae));

}

/**
 * 测试AES加解密
 */
export function testAES() {

    var key = '751f621ea5c8f930751f621ea5c8f930';
    console.log('加密的key:', key);
    var iv = '2624750004598718';
    console.log('加密的iv:', iv);
    var data = "Hello, nodejs. 中文测试  演示aes-256-cbc加密和解密";
    //加密
    var crypted = appServer.encrypt.encryptAES(key, iv, data);
    console.log("数据加密后:", crypted);
    var dec = appServer.encrypt.decryptAES(key, iv, crypted);
    console.log("数据解密后:", dec);

}